package rpc

import (
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

func HttpPost(postData string,class string,method string,parame string) []byte {
	exchangeUrl:="https://www.rightbtc.com/v1/"+class+"/"+method+"/"+parame
	log.Println("请求url：",exchangeUrl)
	client := &http.Client{}
	req, err := http.NewRequest("POST", exchangeUrl, strings.NewReader(postData))
	req.Header.Add("Content-Type", "application/json")
	resp, err := client.Do(req)
	if err != nil {
		log.Fatalln(err)
	}
	defer resp.Body.Close()

	//查看通信是否正常
	//fmt.Println("returnJson:", resp.Status)

	body, _ := ioutil.ReadAll(resp.Body)
	return body
}


func HttpGet(class string,method string,parame string) []byte {
	if class=="" {
		exchangeUrl:="https://www.rightbtc.com/v1/"+method
		//log.Println(exchangeUrl)
		resp, err := http.Get(exchangeUrl)
		defer resp.Body.Close()
		if err != nil {
			log.Fatalln(err)
		}
		body, _ := ioutil.ReadAll(resp.Body)
		return body
	}
	exchangeUrl:="https://www.rightbtc.com/v1/"+class+"/"+method+"/"+parame
	//log.Println(exchangeUrl)
	resp, err := http.Get(exchangeUrl)
	defer resp.Body.Close()
	if err != nil {
		log.Fatalln(err)
	}

	body, _ := ioutil.ReadAll(resp.Body)

	return body

}


func HttpAuth(postData string,md5Data string,class string,method string,apiKey string,timestamp string) []byte{
	//timestamp:=time.Now().Unix()
	//log.Println(timestamp)
	//var s string
	//var symbol string
	exchange_apiurl:="https://www.rightbtc.com/v1/"+class+"/"+method

	client := &http.Client{}

	req, _ := http.NewRequest("POST", exchange_apiurl,strings.NewReader(postData))
	req.Header.Set("Accept-Charst", "UTF-8")
	req.Header.Set("APIKEY",apiKey)
	req.Header.Set("SIGNATURE",md5Data)
	req.Header.Set("NONCE",timestamp)
	req.Header.Set("Content-Type","application/json")
	resp, err := client.Do(req)
	if err != nil {
		log.Fatalln(err)
	}
	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)
	//fmt.Println(string(data))
	return body
}

